package com.open.capacity.prints.service.impl;

import com.open.capacity.prints.util.ChineseYuanUtil;
import com.open.capacity.prints.util.uuid.UUIDUtils;
import com.open.capacity.user.model.SysArea;
import com.open.capacity.user.service.SysAreaService;
import com.open.capacity.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.prints.entity.BillGhywwts;
import com.open.capacity.prints.dao.BillGhywwtsDao;
import com.open.capacity.prints.service.BillGhywwtsService;


@Service
public class BillGhywwtsServiceImpl implements BillGhywwtsService {

    @Autowired
    private BillGhywwtsDao billGhywwtsDao;

    @Autowired
    private SysAreaService areaService;

    /**
     * 添加
     *
     * @param billGhywwts
     */
    public int save(BillGhywwts billGhywwts) {
        billGhywwts.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        billGhywwts.setCreateTime(new Date());

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.parseDate2(billGhywwts.getDjrq().replaceAll("-", "").replaceAll(" ", "")));                    //放入Date类型数据
        billGhywwts.setYear(calendar.get(Calendar.YEAR));                    //获取年份
        billGhywwts.setMonth(String.valueOf(calendar.get(Calendar.MONTH)));                    //获取月份
        billGhywwts.setDay(String.valueOf(calendar.get(Calendar.DATE)));                    //获取日
        billGhywwts.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billGhywwts.getMoney2()));

        return billGhywwtsDao.save(billGhywwts);
    }

    /**
     * 修改
     *
     * @param billGhywwts
     */
    public int update(BillGhywwts billGhywwts) {
        billGhywwts.setCreateTime(new Date());

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.parseDate2(billGhywwts.getDjrq().replaceAll("-", "").replaceAll(" ", "")));                    //放入Date类型数据
        billGhywwts.setYear(calendar.get(Calendar.YEAR));                    //获取年份
        billGhywwts.setMonth(String.valueOf(calendar.get(Calendar.MONTH)));                    //获取月份
        billGhywwts.setDay(String.valueOf(calendar.get(Calendar.DATE)));                    //获取日
        billGhywwts.setMoney1(ChineseYuanUtil.number2CNMontrayUnit(billGhywwts.getMoney2()));

        return billGhywwtsDao.update(billGhywwts);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return billGhywwtsDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public BillGhywwts findById(String id) {
        BillGhywwts item = billGhywwtsDao.findById(id);
        BigDecimal resut = item.getMoney2();
        Integer million5Count = resut.divide(BigDecimal.valueOf(100000000)).intValue();
        item.setMillion5(million5Count);
        resut = resut.subtract(BigDecimal.valueOf(million5Count * 100000000));

        Integer million4Count = resut.divide(BigDecimal.valueOf(10000000)).intValue();
        item.setMillion4(million4Count);
        resut = resut.subtract(BigDecimal.valueOf(million4Count * 10000000));


        Integer million3Count = resut.divide(BigDecimal.valueOf(1000000)).intValue();
        item.setMillion3(million3Count);
        resut = resut.subtract(BigDecimal.valueOf(million3Count * 1000000));

        Integer million2Count = resut.divide(BigDecimal.valueOf(100000)).intValue();
        item.setMillion2(million2Count);
        resut = resut.subtract(BigDecimal.valueOf(million2Count * 100000));

        Integer million1Count = resut.divide(BigDecimal.valueOf(10000)).intValue();
        item.setMillion1(million1Count);
        resut = resut.subtract(BigDecimal.valueOf(million1Count * 10000));

        Integer thousandCount = resut.divide(BigDecimal.valueOf(1000)).intValue();
        item.setThousand(thousandCount);
        resut = resut.subtract(BigDecimal.valueOf(thousandCount * 1000));

        Integer hundredCount = resut.divide(BigDecimal.valueOf(100)).intValue();
        item.setHundred(hundredCount);
        resut = resut.subtract(BigDecimal.valueOf(hundredCount * 100));

        Integer tenCount = resut.divide(BigDecimal.valueOf(10)).intValue();
        item.setTen(tenCount);
        resut = resut.subtract(BigDecimal.valueOf(tenCount * 10));

        Integer yuanCount = resut.divide(BigDecimal.valueOf(1)).intValue();
        item.setYuan(yuanCount);
        resut = resut.subtract(BigDecimal.valueOf(yuanCount * 1));

        Integer jiaoCount = resut.divide(BigDecimal.valueOf(0.1)).intValue();
        item.setJiao(jiaoCount);
        resut = resut.subtract(BigDecimal.valueOf(jiaoCount * 0.1));

        Integer fenCount = resut.divide(BigDecimal.valueOf(0.01)).intValue();
        item.setFen(fenCount);

        if (StringUtils.isNotEmpty(item.getSkrProvince())) {
            SysArea province = areaService.findById(item.getSkrProvince());
            item.setSkrProvinceStr(province == null ? "" : province.getAreaName());
        }
        if (StringUtils.isNotEmpty(item.getSkrCity())) {
            SysArea city = areaService.findById(item.getSkrCity());
            item.setSkrCityStr(city == null ? "" : city.getAreaName());
        }
        if (StringUtils.isNotEmpty(item.getSkrCountry())) {
            SysArea country = areaService.findById(item.getSkrCountry());
            item.setSkrCountryStr(country == null ? "" : country.getAreaName());
        }

        return item;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<BillGhywwts> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<BillGhywwts> list = billGhywwtsDao.findAll(params);
        list.forEach(item->{
            if (StringUtils.isNotEmpty(item.getSkrProvince())) {
                SysArea province = areaService.findById(item.getSkrProvince());
                item.setSkrProvinceStr(province == null ? "" : province.getAreaName());
            }
            if (StringUtils.isNotEmpty(item.getSkrCity())) {
                SysArea city = areaService.findById(item.getSkrCity());
                item.setSkrCityStr(city == null ? "" : city.getAreaName());
            }
            if (StringUtils.isNotEmpty(item.getSkrCountry())) {
                SysArea country = areaService.findById(item.getSkrCountry());
                item.setSkrCountryStr(country == null ? "" : country.getAreaName());
            }
        });
        PageInfo<BillGhywwts> pageInfo = new PageInfo(list);

        return PageResult.<BillGhywwts>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<BillGhywwts> findAll(Map<String, Object> params) {
        return billGhywwtsDao.findAll(params);
    }

}
